// pages/search/search.js
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isfous:true,
    keyWord:'小推车',
    pageNo:1,
    pageSize:10,
    imgurl:app.globalData.Imageurl,
    count:0,
    goodslist:[],
    pages:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGoodsPage(this.data.keyWord)
  },
  getGoodsPage(keyWords){
    var t=this
      wx.request({
        url: app.globalData.Interfaceurl + 'getGoodsPage',
        method: "post",
        header: {
            "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
            userId: app.globalData.userId,
            pageNo: t.data.pageNo,
            pageSize:t.data.pageSize,
            keyWord:keyWords,
        },
        success: res => {
          console.log(res)
          console.log(res.data.page.count)
          console.log(res.data.page.count/t.data.pageSize)
          this.setData({
            pages:Math.ceil(res.data.page.count/t.data.pageSize)
          })
          if (t.data.goodslist.length>0) {
            t.setData({
              goodslist:t.data.goodslist.concat(res.data.page.list),
              count:res.data.page.count
            })
          }else{
            t.setData({
              goodslist:res.data.page.list,
              count:res.data.page.count
            })
          }
         
        }

      })
},

  //去详情
  goshopxq(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/details/details?goodsid='+e.currentTarget.dataset.goodsid,
    })
  },
  bindinput(e){
    console.log(e)
    this.setData({
      goodslist:[]
    })
    
    if (e.detail.value!='') {
      this.setData({
        keyWords:e.detail.value
      })

      this.getGoodsPage(this.data.keyWords)
    }
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.pageNo)
    console.log(this.data.pages)
     if (this.data.pageNo<this.data.pages) {
       this.setData({
         pageNo:this.data.pageNo+1
       })
       
       
       this.getGoodsPage(this.data.keyWords)
     }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})